//
//  JDShowAllMenuView.swift
//  DragonWallet
//
//  Created by volantPig on 2024/4/3.
//

import UIKit

class JDShowAllMenuView: UIView {

    var bgImgV: UIImageView?
//    var arrowImgV: UIImageView?
    var menuTableView: UITableView?
    var menuTitle = ["用户下载码"]//"车辆下载码",
    var selectedCompleted: ((Int)->())?
    
    override init(frame: CGRect) {
//        super.init(frame: frame)
        super.init(frame: UIScreen.main.bounds)
        // TODO: 找一下，把getKeywindows方法迁移过来
        JDTools.getKeyWindows()?.rootViewController?.view.addSubview(self)
        self.backgroundColor = .black.withAlphaComponent(0.2)
        self.setupUIElements()
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(removeSelf))
        self.addGestureRecognizer(tap)
    }
    
    func setupUIElements() {
        self.bgImgV = UIImageView()
        self.addSubview(self.bgImgV!)
        self.bgImgV?.snp.makeConstraints({ make in
            make.right.equalTo(self).offset(-8)
            make.top.equalTo(self).offset(UIDevice().safeAreaInset.top)
            make.height.equalTo(53*(self.menuTitle.count)+10)
            make.width.equalTo(113)
        })
        self.bgImgV?.backgroundColor = .clear
        self.bgImgV?.isUserInteractionEnabled = true
        self.bgImgV?.makeCorner(10)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapActionBBBBB))
        self.bgImgV?.addGestureRecognizer(tap)
        
        let topImageV = UIImageView()
        self.bgImgV?.addSubview(topImageV)
        topImageV.snp.makeConstraints { make in
            make.centerX.equalTo(self.bgImgV!).offset(30)
            make.top.equalTo(self.bgImgV!)
            make.height.equalTo(12)
        }
        topImageV.contentMode = .scaleAspectFit
        topImageV.image = UIImage(named: "arrow_top")?.withRenderingMode(.alwaysOriginal)
        
        
        self.menuTableView = UITableView(frame: .zero, style: .plain)
        self.bgImgV?.addSubview(self.menuTableView!)
        self.menuTableView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.bgImgV!)
            make.top.equalTo(self.bgImgV!).offset(10)
            make.bottom.equalTo(self.bgImgV!)
        })
        self.menuTableView?.delegate = self
        self.menuTableView?.dataSource = self
        self.menuTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
        self.menuTableView?.separatorStyle = .none
        self.menuTableView?.isScrollEnabled = false
        self.menuTableView?.makeCorner(10)
        
       
    }
    
    @objc func removeSelf() {
        self.removeFromSuperview()
    }
    
    @objc func tapActionBBBBB() {
        self.selectedCompleted?(0)
        self.removeFromSuperview()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension JDShowAllMenuView: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.menuTitle.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 53
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell")
        
        for subV in cell?.contentView.subviews ?? [UIView]() {
            subV.removeFromSuperview()
        }
        
        let label = UILabel()
        cell?.contentView.addSubview(label)
        label.snp.makeConstraints { make in
            make.left.top.right.bottom.equalTo(cell!.contentView)
        }
        label.text = self.menuTitle[indexPath.row]
        label.textAlignment = .center
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        self.selectedCompleted?(indexPath.row)
        self.removeFromSuperview()
    }
}
